package controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.Advertisement;
import pojo.Merchant;
import service.AdvertisementService;

@Controller
public class AdvertisementController {

	@Resource(name="advertisementServiceImpl")
	private AdvertisementService advertisementService;

	/*@RequestMapping(value="/advertisement/",method = RequestMethod.POST)
	public String save(Advertisement ad,HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		advertisementService.save(ad, merchant.getId());
		return "adInsert";
	}*/

	@ResponseBody
	@RequestMapping(value="/advertisement/status",method = RequestMethod.PUT)
	public Advertisement statusChange(String advertisementId,String status,HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		Advertisement ad = advertisementService.update(advertisementId, status, merchant.getId());
		return ad;
	}
	
	@ResponseBody
	@RequestMapping(value="/views/advertisement/all",method = RequestMethod.GET)
	public List<Advertisement> findAll(HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		List<Advertisement> list = (List<Advertisement>) advertisementService.findAll(merchant.getId());
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/views/advertisement/success",method = RequestMethod.GET)
	public List<Advertisement> findByStatus(HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		List<Advertisement> list = (List<Advertisement>) advertisementService.findByStatus(merchant.getId(),"pass");
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/advertisement",method = RequestMethod.POST)
	public String sendJms(Advertisement ad,HttpSession session,String time, String date){
		System.out.println(time);
		System.out.println(date);
		System.out.println("ad price"+ad.getAdPrice());
		System.out.println("foodId"+ad.getFoodId());
		Merchant merchant = (Merchant) session.getAttribute("user");
		return advertisementService.sendJms(ad,merchant,date,time);
	}
	
	@RequestMapping(value="/views/advertisement",method = RequestMethod.GET)
	public String toJsp(Model model,HttpSession session){
		model.addAttribute("merchant", session.getAttribute("user"));
		return "WEB-INF/advertisement/advertisementList";
	}
	
}
